package hellfire.platform.graphics.animtools;

import android.view.MotionEvent;
import hellfire.platform.input.TouchListener;
import hellfire.system.graphics.GLDrawable;
import hellfire.system.graphics.GLVec2;

public class SlideEnter extends AbstractSequence implements TouchListener
{

	public enum Direction { UP, DOWN, LEFT, RIGHT };
	public enum Mode { ENTER, LEAVE };
	
	private Direction dir;
	private Mode mode;
	private int sequence = 0;
	private GLVec2 pos;
	
	private Runnable onFinishEvent = null;
	private boolean isFinished = false;
	
	public SlideEnter(GLDrawable drawable, Direction dir, Mode mode) 
	{
		super(drawable);
		this.dir = dir;
		this.mode = mode;
		pos = drawable.getPos();
	}
	

	@Override
	public void progress() 
	{
		if(isFinished) return;
		sequence++;
		if(sequence > 80) 
		{
			if(mode == Mode.ENTER) setPos(pos);
			
			isFinished = true;
			if(onFinishEvent != null)
				onFinishEvent.run();
			
			return;
		}
		
		GLVec2 newPos = null;
		if(mode == Mode.LEAVE)
		{
			float add = sequence * sequence * 0.0008f;
			if(dir == Direction.DOWN)
			{
				newPos = new GLVec2(pos.x(), pos.y()-add);
			}
			if(dir == Direction.UP)
			{
				newPos = new GLVec2(pos.x(), pos.y()+add);
			}
			if(dir == Direction.LEFT)
			{
				newPos = new GLVec2(pos.x()-add, pos.y());
			}
			if(dir == Direction.RIGHT)
			{
				newPos = new GLVec2(pos.x()+add, pos.y());
			}
		}
		else if(mode == Mode.ENTER)
		{
			float add = (sequence - 80) * (sequence - 80) * 0.0008f;
			if(dir == Direction.DOWN)
			{
				newPos = new GLVec2(pos.x(), pos.y()-add);
			}
			if(dir == Direction.UP)
			{
				newPos = new GLVec2(pos.x(), pos.y()+add);
			}
			if(dir == Direction.LEFT)
			{
				newPos = new GLVec2(pos.x()-add, pos.y());
			}
			if(dir == Direction.RIGHT)
			{
				newPos = new GLVec2(pos.x()+add, pos.y());
			}
		}
		else newPos = new GLVec2(0,0);
		drawable.setPos(newPos);
	}

	public boolean isFinished()
	{
		return isFinished;
	}
	
	
	public void setOnFinishEvent(Runnable event)
	{
		this.onFinishEvent = event;
	}
	
	@Override
	public void onTouchEvent(MotionEvent e, GLVec2 pos) 
	{
		if(drawable instanceof TouchListener)
			((TouchListener) drawable).onTouchEvent(e, pos);
	}


	@Override
	public boolean testPosition(GLVec2 pos) 
	{
		if(drawable instanceof TouchListener)
			return ((TouchListener) drawable).testPosition(pos);
		return false;
	}
	

}
